// 网关配置的前缀
var companyServer = "/companySer"

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    let accountStatus = $("#accountStatus").html();
    if(accountStatus !== "正常"){
        $("#forbidden").removeAttr("hidden");
    }

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        companyServer = data.companyServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    getMemberInfo();
}

function getMemberInfo() {
    $.ajax({
        type: "GET",
        async: false,
        url: companyServer + "/company/memberInfo",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {

            //清空数据
            $("#memberList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {

                $("#memberList").append(
                    "<tr class='job-application-item'>"+
                    "<td class='application-job'>"+
                    "<h6 class='show-"+i+"' id='show-name-"+i+"'>" + item.typeName + "</h6>"+
                    "<input class='editor-"+i+"' value='" + item.typeName + "' id='editor-name-"+i+"' hidden>"+
                    "</td>"+

                    "<td class='application-employer'>"+
                    "<span class='show-"+i+"' id='show-hr-"+i+"'>" + item.hrNumber + "</span>"+
                    "<input class='editor-"+i+"' value='" + item.hrNumber + "' id='editor-hr-"+i+"' hidden>"+
                    "</td>"+

                    "<td class='application-created'>"+
                    " <span class='show-"+i+"' id='show-post-"+i+"'>" + item.postNumber + "</span>"+
                    "<input class='editor-"+i+"' value='" + item.postNumber + "' id='editor-post-"+i+"' hidden>"+
                    "</td>"+

                    "<td class='application-created'>"+
                    "<span class='show-"+i+"' id='show-price-"+i+"'>" + item.price + "</span>"+
                    "<input class='editor-"+i+"' value='" + item.price + "' id='editor-price-"+i+"' hidden>"+
                    "</td>"+

                    "<td class='view-application-pop text-right'>"+
                    "<a class='show-"+i+"' onclick='updateMemberInfo(\""+i+"\")'><span>修改</span></a>"+
                    "<a class='editor-"+i+"' onclick='cancelUpdate(\""+i+"\")' hidden='hidden'><span>取消</span></a>"+
                    "<a class='editor-"+i+"' onclick='saveMemberInfo(\""+i+"\","+item.memberTypeId+")' hidden='hidden'><span>保存</span></a>"+
                    "</td>"+
                    "</tr>"
                )

            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

function updateMemberInfo(i){
    $(".show-"+i).attr("hidden", "hidden");
    $(".editor-"+i).removeAttr("hidden");
}

function cancelUpdate(i){
    $(".editor-"+i).attr("hidden", "hidden");
    $(".show-"+i).removeAttr("hidden");
}

function saveMemberInfo(i, memberTypeId){
    let name = $("#editor-name-"+i).val();
    let hr = $("#editor-hr-"+i).val();
    let post = $("#editor-post-"+i).val();
    let price = $("#editor-price-"+i).val();
    $.confirm({
        title: '提示',
        content: '确认修改该会员信息嘛？',
        type: 'green',
        theme: "modern",
        closeAnimation: 'scaleY',
        buttons: {
            ok: {
                text: '确认',
                btnClass: 'btn-warning',
                action: function () {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: companyServer + "/company/memberInfo",
                        headers: {
                            "Content-Type": "application/json",
                            "Authorization": Authorization,
                            "refreshToken": refreshToken,
                        },
                        data: JSON.stringify({
                            "memberTypeId": memberTypeId,
                            "typeName": name,
                            "price": price,
                            "postNumber": post,
                            "hrNumber": hr,
                        }),
                        success: function (data) {
                            unauthorized(data);
                            if (data.resp_code === 0) {
                                // 成功
                                cocoMessage.info(2000, data.resp_msg);

                                $("#show-name-"+i).html(name);
                                $("#show-hr-"+i).html(hr);
                                $("#show-post-"+i).html(post);
                                $("#show-price-"+i).html(price);

                                cancelUpdate(i);
                            } else {
                                // 失败
                                cocoMessage.error(2000, data.resp_msg, function () {
                                    console.log(data.resp_msg);
                                });
                            }
                        },
                        error: function (err) {
                            // 异常
                            cocoMessage.error(2000, err.statusText);
                        }
                    })
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-default'
            }
        }
    });
}